import 'package:dio/dio.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';

part 'example.g.dart';

@RestApi(baseUrl: 'http://localhost:3000/')
abstract class RestClient {
  factory RestClient(Dio dio, {String? baseUrl}) = _RestClient;

  @GET('/tasks')
  Future<List<Task>> getTasks();

  @GET('/tasks/{id}')
  Future<Task> getTask(@Path() String id);

  @MetaData(
    appType: 'android',
    language: 'en'
  )
  @GET('/profiles')
  Future<List<User>> getUsers();
}

class MetaData extends TypedExtras {
  final String? appType;
  final String? language;

  const MetaData({this.appType, this.language});
}

@JsonSerializable()
class Task {
  final String? id;
  final String? name;
  final String? createdAt;

  const Task({this.id, this.name, this.createdAt});

  factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json);

  Map<String, dynamic> toJson() => _$TaskToJson(this);

  @override
  String toString() {
    return 'Task{id: $id, name: $name, createdAt: $createdAt}';
  }
}

@JsonSerializable()
class User {
  final int id;
  final String email;

  User({required this.id, required this.email});

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

  Map<String, dynamic> toJson() => _$UserToJson(this);

   @override
   String toString() {
     return 'Profile{id: $id, email: $email}';
   }
}